package controller;

import bean.WangEditorResult;
import com.github.pagehelper.PageInfo;
import domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import service.ProductService;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //查询产品所有信息
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "5") int pageSize, @RequestParam(defaultValue = "1") int pageNum){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Product> allProduct = productService.findAllProduct(pageSize, pageNum);
        modelAndView.getModel().put("page",allProduct);
        modelAndView.setViewName("product_all");
        return modelAndView;
    }
    //通过id查询信息
    @RequestMapping("/information")
    public ModelAndView information(int id){
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findProductById(id);
        modelAndView.getModel().put("product",product);
        modelAndView.setViewName("product_information");
        return modelAndView;
    }
    //新增产品
    @RequestMapping("/add")
    public String add(Product product, HttpServletRequest request){
        productService.addProduct(product);
        return "redirect:"+request.getContextPath()+"/product/all";
    }
    //富文本图片上传
    @RequestMapping("/upload")
    public @ResponseBody WangEditorResult upload3(HttpServletRequest request, MultipartFile file) throws Exception {
        //创建文件夹
        ServletContext servletContext = request.getSession().getServletContext();
        String realPath = servletContext.getRealPath("/upload");
        System.out.println(realPath);
        File folder = new File(realPath);
        if (!folder.exists()){
            folder.mkdirs();
        }
        //获取文件名
        String filename = file.getOriginalFilename();
        filename = UUID.randomUUID()+"_"+filename;
        //文件夹中创建一个空文件夹
        File file1 = new File(folder,filename);
        //将上传文件写入空文件中
        file.transferTo(file1);
        WangEditorResult wangEditorResult = new WangEditorResult();
        wangEditorResult.setErrno(0);
        String[] data = {"/upload/"+filename};
        wangEditorResult.setData(data);
        return wangEditorResult;
    }
    //修改产品信息
    @RequestMapping("/update")
    public String update(Product product,HttpServletRequest request){
        productService.updateProduct(product);
        return "redirect:"+request.getContextPath()+"/product/all";
    }
    //修改产品信息时进行的查询
    @RequestMapping("/updateInformation")
    public ModelAndView updateInformation(int id){
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findProductById(id);
        modelAndView.getModel().put("product",product);
        modelAndView.setViewName("product_update");
        return modelAndView;
    }
    //删除产品
    @RequestMapping("/del")
    public String  del(int[] id,@RequestHeader String referer,HttpServletRequest request){
        productService.delProduct(id);
        return "redirect:"+request.getContextPath()+referer;
    }

    //修改产品状态
    @RequestMapping("/updateStatus")
    public String updateStatus(int[] id,int status,@RequestHeader String referer){
        productService.updateProductStatus(id,status);
        return "redirect:"+referer;
    }

    @RequestMapping(value = "/uploadImg",produces = "text/plain;charset=utf-8")
    public @ResponseBody String uploadImg(HttpServletRequest request, MultipartFile file) throws Exception {
        //创建文件夹
        ServletContext servletContext = request.getSession().getServletContext();
        String realPath = servletContext.getRealPath("/upload");
        File folder = new File(realPath);
        if (!folder.exists()){
            folder.mkdirs();
        }
        //获取文件名
        String filename = file.getOriginalFilename();
        filename = UUID.randomUUID()+"_"+filename;
        //文件夹中创建一个空文件夹
        File file1 = new File(folder,filename);
        //将上传文件写入空文件中
        file.transferTo(file1);
        return "/upload/"+filename;
    }
}
